package test.movies;

import static org.openqa.selenium.support.PageFactory.initElements;
import static org.openqa.selenium.support.ui.ExpectedConditions.*;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class AddDescription extends PageMovies {
	

    // Logger variable
    private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(AddDescription.class);
   
   
    //Form with filling of data on New Movie Page
     private String information = "//div[@class='addmovie']";
        
    // Field with title on New Movie page 
    private String fieldTitle =  "//input[@name='name']";

    // Field with year on New Movie page 
    private String fieldYear =  "//input[@name='year']";
           
    // Button Submit on New Movie page 
    private String fieldSave =  "//input[@id='submit']";
      
    // Element shows that field is mandatory for filling    
    private String allMandatories = "//label[@class='error']";
    
    
    /**
     * Constructor of the MainPage class
     * @param driver - configured driver
     */
    public AddDescription(WebDriver driver) {
        super(driver);
        initElements(driver, this);
    }

    /**
     * Check if it is the right page
     * @return true if right page
     */
    public boolean checkIfNewDescriptionIsOpen() {

        LOGGER.info("Checking whether the user on the New Description Page");
        try{

            WebElement checkPage = new WebDriverWait(driver,20).until(visibilityOfElementLocated(By.xpath(information)));
            LOGGER.info("New Description Page is open");

            return true;
        }
        catch (NullPointerException e){
            LOGGER.error("Main Page is disable!!!");
            return false;}

        catch (TimeoutException e){

            LOGGER.error("New Description Page is not open!!!");
            return false;
        }

    }

    // this method fill in the mandatory fields

    public boolean insertMovieData(String title, String year) {

        LOGGER.info("Insert Movie data");

        try {
          

            WebElement titleInsert = wait.until(elementToBeClickable(By.xpath(fieldTitle)));
            LOGGER.info("Insert Title");
            titleInsert.click();
            titleInsert.sendKeys(Keys.CONTROL + "a");
            titleInsert.sendKeys(Keys.DELETE);
            titleInsert.sendKeys(title);

            WebElement yearInsert = wait.until(elementToBeClickable(By.xpath(fieldYear)));
            LOGGER.info("Insert Year");
            yearInsert.click();
            yearInsert.sendKeys(Keys.CONTROL + "a");
            yearInsert.sendKeys(Keys.DELETE);
            yearInsert.sendKeys(year);

            return true;
        }
        catch (NullPointerException e){
            LOGGER.error("New Description Page is disable");
            return false;
        }
        catch (TimeoutException e){
            LOGGER.error("Can't find field to fill " + e.toString());
            return false;
        }

    }

    // this method checks if all mandatory fields are filled
    public boolean checkIfRequiredDataIsSet() {

        LOGGER.info("Check if all required data are filled");

        try {
        	
            WebElement field1 = new WebDriverWait(driver,10).until(presenceOfElementLocated(By.xpath("//label[@for='name']")));
              if (field1.isDisplayed()) {
                  LOGGER.error("The mandatory field Title is empty!!!");
                 return false;
              }

            WebElement field2 = new WebDriverWait(driver,10).until(presenceOfElementLocated(By.xpath("//label[@for='year']")));
            if (field2.isDisplayed()) {
                LOGGER.error("The mandatory field Year is empty or invalid!!!");
                return false;
            }
            else {
                LOGGER.info("All mandatory fields are filled");
                return true;  }
    }
    catch (NullPointerException e){
        LOGGER.error("New Description Page is disable");
        return false;}

    catch (TimeoutException e){

        LOGGER.info("All mandatory fields are filled");
        return false;
    }
   
}

 // this method save new description
    public MovieInfo createNewDescription(){

        try{

            WebElement saveMovie = new WebDriverWait(driver,20).until(visibilityOfElementLocated(By.xpath(fieldSave)));
            saveMovie.click();
            LOGGER.info("Save New Description");

            return new MovieInfo(driver);
        }
        catch (NullPointerException e){
            LOGGER.error("New Description Page is disable!!!");
            return null;}

        catch (TimeoutException e){

            LOGGER.error("Button for saving is not found!!!");
            return null;
        }


    }

    
}
